//
//  AXHomeZjzdVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeZjzdVC.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHomeZjzdSecondCell.h"
#import "UIViewController+HBD.h"
#import "CollectionCategoryModel.h"
#import "LeftTableViewModel.h"
#import "AXHomeZjzdCropCategoryModel.h"
#import "AXHomeZjzdDetailsVC.h"
#import "CommitProblemViewController.h"
#import "WHRPublishPostARewardVC.h"
#import "WHRDiagnoseScheduleVC.h"

@interface AXHomeZjzdVC ()<UITableViewDelegate,UITableViewDataSource>



@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<AXHomeZjzdSecondModel *>  *modelsArray;

/** 第一个数组 */
@property (nonatomic, copy) NSMutableArray<AXHomeZjzdCropCategoryModel *> *oneArray;
/** 第二个数组 */
@property (nonatomic, copy) NSMutableArray<LeftTableViewModel *> *twoArray;
/** 第三个数组 */
@property (nonatomic, copy) NSMutableArray<CollectionCategoryModel *> *threeArray;
/** 第四个数组 */
@property (nonatomic, copy) NSArray *fourArray;

/** 擅长作物ID beGoodAtCropsId */
@property (nonatomic , strong) NSString *beGoodAtCropsId;
/** beGoodAtFieldId 擅长领域ID */
@property (nonatomic , strong) NSString *beGoodAtFieldId;
/** 分页索引 */
@property (nonatomic, assign) NSInteger pageIndex; //页数

@property (nonatomic, assign) NSInteger currentOneIndex;
@property (nonatomic, assign) NSInteger currentTwoLeftIndex;
@property (nonatomic, assign) NSInteger currentTwoRightIndex;
@property (nonatomic, assign) NSInteger currentThreeIndex;

@property (nonatomic , strong) UIView *leftView;
@property (nonatomic , strong) UIView *rightView;

@end

@implementation AXHomeZjzdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _fourArray = @[@"排行榜",@"职称",@""];
    // Do any additional setup after loading the view.
    [self setupNavigationItems];
    [self creatView];
    [self setlayout];
    /** 导航条白色 */
    self.hbd_barTintColor = [UIColor whiteColor];
    self.hbd_barShadowHidden = YES;
    
    /** 调用作物问题接口 */
    [self requestCategoryList];
    /** 调用作物分类接口 */
   // [self requestStairList];
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestList];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)creatView {
    
    
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXHomeZjzdSecondCell class] forCellReuseIdentifier:[AXHomeZjzdSecondCell reuseID]];
    [self.view addSubview:self.tableView];
    
   
    
}



- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(Wi)
    .heightIs(TableViewHeight);
    
    
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"专家诊断";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - requestNetwork

/** 问题分类 */

- (void)requestCategoryList {

    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleCrop, URL_AgricultureCircleCrop_selectExcelDomain) success:^(id respose) {

        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {

            weakSelf.oneArray = [AXHomeZjzdCropCategoryModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"]];
            
          
 
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {

        }

    } failure:^(NSError *error) {

    }];

}

/** 专家列表接口 */

- (void)requestList {
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"beGoodAtCropsId"] = self.beGoodAtCropsId;
    params[@"beGoodAtFieldId"] = self.beGoodAtFieldId;
    params[@"queryRankType"] = @(_currentThreeIndex);
    params[@"pageSize"] = @15;
    params[@"pageNum"] = @(self.pageIndex);
    
    

    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectExpertUserList) success:^(id respose){
     
     if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
        
        weakSelf.modelsArray = [AXHomeZjzdSecondModel  mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
        [self.tableView reloadData];
        
    } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
        
    }
     
    
     [self.tableView.mj_header endRefreshing];
     [self.tableView.mj_footer endRefreshing];
     
     } failure:^(NSError *error) {
         
         if (self.pageIndex > 1) {
             self.pageIndex--;
         }
         
         [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer endRefreshing];
         
         
     }];
    
}


#pragma mark - tableViewDelegate & tableViewDatasorce

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     AXHomeZjzdSecondModel *model = self.modelsArray[indexPath.row];
    AXHomeZjzdDetailsVC *vc = [[AXHomeZjzdDetailsVC alloc]init];
    vc.fExpertId = model.fUserId;
    vc.fProfilePhoto = model.fProfilePhoto;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    headView.backgroundColor = [UIColor whiteColor];
    
    self.leftView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 80, 80)];
    self.leftView.backgroundColor = [UIColor whiteColor];
    [headView addSubview:self.leftView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fbAction:)];
    [self.leftView addGestureRecognizer:tap];
    
    
    UIImageView *fb = [[UIImageView alloc]init];
    fb.userInteractionEnabled = YES;
    fb.image = [UIImage imageNamed:@"zjzd_fbxs"];
    [self.leftView addSubview:fb];
    
   
    
    fb.sd_layout
    .leftSpaceToView(self.leftView, 0)
    .topSpaceToView(self.leftView, 0)
    .widthIs(40)
    .heightIs(45);
    
    UILabel *fbLabel = [[UILabel alloc]init];
    fbLabel.text = @"发布悬赏";
    fbLabel.font = FONT_FOURTEEN;
    fbLabel.textColor = COLOR_DIV;
    [self.leftView addSubview:fbLabel];
    
    fbLabel.sd_layout
    .leftSpaceToView(self.leftView, 0)
    .topSpaceToView(fb, 10)
    .widthIs(80)
    .heightIs(20);
    
    
    
    
    self.rightView = [[UIView alloc]initWithFrame:CGRectMake(120, 20, 80, 80)];
    self.rightView.backgroundColor = [UIColor whiteColor];
    [headView addSubview:self.rightView];
    
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zdAction:)];
    [self.rightView addGestureRecognizer:tap1];
    
    UIImageView *zd = [[UIImageView alloc]init];
    zd.image = [UIImage imageNamed:@"zjzd_zdjd"];
    [self.rightView addSubview:zd];
    
    zd.sd_layout
    .leftSpaceToView(self.rightView, 0)
    .topSpaceToView(self.rightView, 0)
    .widthIs(40)
    .heightIs(45);
    
    UILabel *zdLabel = [[UILabel alloc]init];
    zdLabel.text = @"诊断进度";
    zdLabel.font = FONT_FOURTEEN;
    zdLabel.textColor = COLOR_DIV;
    [self.rightView addSubview:zdLabel];
    
    zdLabel.sd_layout
    .leftSpaceToView(self.rightView, 0)
    .topSpaceToView(zd, 10)
    .widthIs(80)
    .heightIs(20);
    
    
   
    
    
    
    
    
    return headView;
    
    
}

#pragma mark - action

- (void)fbAction:(UIView *)view {
    
    
    
    WHRPublishPostARewardVC *vc = [[WHRPublishPostARewardVC alloc]init];
   // vc.cropId = self.beGoodAtCropsId;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)zdAction:(UIView *)view {
    
    WHRDiagnoseScheduleVC *VC = [[WHRDiagnoseScheduleVC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
    
}

#pragma mark - action


/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 150;
    
}




//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeZjzdSecondCell class] contentViewWidth:SCREEN_WIDTH];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AXHomeZjzdSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeZjzdSecondCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = self.modelsArray[indexPath.row];
    

    /** 立即咨询的按钮方法 */
    
    cell.tapCelllickedBlock = ^(NSInteger index) {
        
        CommitProblemViewController *vc = [[CommitProblemViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    };
    
        
   
   
    return cell;
    
    
}


#pragma mark - setter & getter

- (NSMutableArray<AXHomeZjzdSecondModel *> *)modelsArray {
    
    if (!_modelsArray) {
        _modelsArray = [NSMutableArray<AXHomeZjzdSecondModel *> array];
    }
    return _modelsArray;
}









@end

